package graduationorder.graduationorder.service.impl;

import graduationorder.graduationorder.Unit.OrderIdGenerator;
import graduationorder.graduationorder.domain.Order;
import graduationorder.graduationorder.error.BusinessException;
import graduationorder.graduationorder.error.EmBusinessException;
import graduationorder.graduationorder.mapper.OrdersMapper;
import graduationorder.graduationorder.result.CommonReturnType;
import graduationorder.graduationorder.service.OrdersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class OrdersServiceimpl implements OrdersService {

    @Resource
    private OrdersMapper ordersmapper;

    @Override
    public CommonReturnType getOrders(String orderId) {

        Order order = ordersmapper.selectByPrimaryKey(orderId);

        if(order==null){
            return CommonReturnType.create("");
        }
        return CommonReturnType.create(order);
    }

    /**
     *
     * @return 获取所有订单信息
     */
    public CommonReturnType getAllOrdersmapper() {



        List<Order> orders = ordersmapper.selectAllOrders();
        if (orders.isEmpty()){
            return CommonReturnType.create("");
        }
        return CommonReturnType.create(orders);
    }

    /**
     *
     * @return 创建订单
     */
    public CommonReturnType createOrder(Order order) throws BusinessException {
        // 生成订单ID
        String orderId = OrderIdGenerator.generateOrderId();
        order.setOrderId(orderId);

        // 假设其他字段已经设置好
        // 设置orderDate为当前日期
        order.setOrderDate(new Date());

        // 插入订单
        int result=ordersmapper.insertOrder(
                order.getOrderId(),
                order.getCustomerSource(),
                order.getServiceType(),
                order.getMajor(),
                order.getTitle(),
                order.getWordCount(),
                order.getOrderDate(),
                order.getDueDate(),
                order.getAdditionalNotes(),
                order.getStatus()
        );
        if(result <= 0){
            throw new BusinessException(EmBusinessException.SERVER_ERROR);
        }
        return CommonReturnType.create(result);
    }
    /**
     *
     * @return 删除订单
     */
    @Override
    public CommonReturnType deleteOrder(String orderId) throws BusinessException {
        int result=ordersmapper.deleteOrderById(orderId);
        if (result <= 0){
            throw new BusinessException(EmBusinessException.SERVER_ERROR);
        }
        return CommonReturnType.create(result);
    }

    /**
     *
     * @param order
     * @return 更改订单信息
     */
    @Override
    public CommonReturnType updateOrder(Order order) throws BusinessException {
        String orderId = order.getOrderId();
        int result=ordersmapper.updateOrder(orderId);//有问题
        if(result <= 0){
            throw new BusinessException(EmBusinessException.SERVER_ERROR);
        }
        return CommonReturnType.create(result);
    }

    /**
     *
     * @return 模糊查询
     */
    @Override
    public CommonReturnType findOrdersByCriteria(String orderId, String customerSource, String serviceType, String major, String title, Integer wordCount, String orderDate, String dueDate) {
        List<Order> ordersByCriteria = ordersmapper.findOrdersByCriteria(orderId, customerSource, serviceType, major, title, wordCount, orderDate, dueDate);
        if (ordersByCriteria.isEmpty()){
            return CommonReturnType.create("");
        }
        return CommonReturnType.create(ordersByCriteria);
    }


}
